package com.ecube.maintenance.components.widget;

import android.app.Activity;
import android.content.Context;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.Spinner;
import android.widget.TextView;

import com.ecube.maintenance.R;
import com.ecube.maintenance.components.fragment.CarSelectorFragment;
import com.ecube.maintenance.pojos.UserInfo;
import com.ecube.maintenance.utils.Bog;
import com.ecube.maintenance.utils.StringUtil;


public class UserInfoView extends LinearLayout {
    private Context mContext;
    private UserInfo mUserInfo;
    private EditText ownerName;
    private EditText carNO;
    private TextView phone;
    private TextView model;
    private Spinner provinceSpinner;
    private ArrayAdapter provinceAdapter;
    private TextView submitView;
    private String[] provinceData = new String[]{
            "川", "京", "津", "冀", "晋", "蒙",
            "辽", "吉", "黑", "沪", "苏",
            "浙", "皖", "闽", "赣", "鲁",
            "豫", "鄂", "湘", "粤", "桂",
            "琼", "渝", "贵", "云", "藏",
            "陕", "甘", "青", "宁", "新", "港", "澳", "台"
    };

    public UserInfoView(Context context) {
        super(context);
        this.mContext = context;
        drawView();
    }

    public UserInfoView(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.mContext = context;
        drawView();
    }

    public UserInfoView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.mContext = context;
        drawView();
    }

    private void drawView() {
        LayoutInflater.from(mContext).inflate(R.layout.view_user_info, this);
        initView();
        setData();
    }

    private void initView() {

        mUserInfo = UserInfo.getInstance();
        ownerName = (EditText) findViewById(R.id.ict_owner_name);
        carNO = (EditText) findViewById(R.id.carNO);
        phone = (TextView) findViewById(R.id.phone_number);
        model = (TextView) findViewById(R.id.ict_model);

        provinceSpinner = (Spinner) findViewById(R.id.province_sel_spinner);
        provinceAdapter = new ArrayAdapter<String>(mContext,
                R.layout.item_list_province_spinner, provinceData);
        provinceSpinner.setAdapter(provinceAdapter);

        submitView = (TextView) findViewById(R.id.btn_switch_car_type);
        submitView.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                ((Activity) mContext).getFragmentManager().beginTransaction()
                        .replace(R.id.container, CarSelectorFragment.newInstance())
                        .addToBackStack(null).commit();
            }
        });
    }

    public UserInfo getLatestUserInfo() {
        mUserInfo = UserInfo.getInstance();
        mUserInfo.setRealName(getCurRealName());
        mUserInfo.setCarNO(getCurCarNO());
        return mUserInfo;
    }

    public String getCurRealName() {
        return ownerName.getText().toString();
    }

    public String getCurCarNO() {
        return provinceData[provinceSpinner.getSelectedItemPosition()] + carNO.getText().toString();
    }

    public void setData() {
        ownerName.setText(mUserInfo.getRealName());
        if (TextUtils.isEmpty(mUserInfo.getCarNO())) {
            carNO.setText(mUserInfo.getCarNO());
        } else {
            carNO.setText(mUserInfo.getCarNO().substring(1));
            for (int i = 0; i < provinceData.length; i++) {
                if (provinceData[i].equals(mUserInfo.getCarNO().charAt(0) + "")) {
                    provinceSpinner.setSelection(i);
                    break;
                }
            }
        }
        phone.setText(mUserInfo.getMobile());
        if (TextUtils.isEmpty(mUserInfo.getCarBrand())) {
            model.setVisibility(View.GONE);
            submitView.setText("选择车型");
        } else {
            submitView.setText("更改车型");
            model.setText(mUserInfo.getCarBrand() + "-"
                    + mUserInfo.getCarSerial() + "\n"
                    + mUserInfo.getCarModel());
        }

    }

    public boolean checkUserInput() {
        UserInfo userInfo = getLatestUserInfo();
        if (TextUtils.isEmpty(userInfo.getRealName())) {
            Bog.toast("请输入姓名");
            return false;
        }
        if (!StringUtil.isCarBrand(userInfo.getCarNO())) {
            Bog.toast("请输入正确的车牌号");
            return false;
        }

        if (TextUtils.isEmpty(userInfo.getCarFactory())
                || TextUtils.isEmpty(userInfo.getCarBrand())
                || TextUtils.isEmpty(userInfo.getCarModel())
                || TextUtils.isEmpty(userInfo.getCarSerial())
                ) {
            Bog.toast("请选择车辆资料");
            return false;
        }

        return true;
    }
}
